package com.baseandroid.utils;

import com.androidbase.utils.ToastUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: Routee
 * @date ${DATE}
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */
public class TimeUtils {
    public static String[] Week_DAYS = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    /**
     * /**
     * @param str     需要转换的时间
     * @param pattern 转换成的时间格式
     * @param pattern1  传入的时间格式
     * @return
     */
    public static String getDate(String str,String pattern1, String pattern) {

        SimpleDateFormat sdfl = new SimpleDateFormat(pattern1);
        SimpleDateFormat sdfs = new SimpleDateFormat(pattern);
        String result = str;
        try {
            Date date = sdfl.parse(str);
            result = sdfs.format(date);
        } catch (ParseException e) {
        }
        if (result.length() == pattern.length()) {
            return result;
        }
        SimpleDateFormat sdfm = new SimpleDateFormat(pattern1);
        try {
            Date date2 = sdfm.parse(result);
            result = sdfs.format(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     *
     * @param dateType  日期格式
     * @return  当前日期的指定格式
     */
    public static String getNowDate(String dateType ,int offset){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, offset);
        String day = new SimpleDateFormat(dateType).format(cal.getTime());
        return day;
    }

    /**
     *
     * @param day  当前日期
     * @param format  日期格式
     * @return  是否为当天
     */
    public static boolean isToday(String day , String format) {
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat(format).parse(day);
        } catch (ParseException e) {
            ToastUtils.showLongToast("日期解析异常");
        }
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * @param n       延迟n天
     * @param pattern 要返回的日期格式
     * @return 返回n天后的字符串
     */
    public static String getDateNDaysDelay(int n, String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, n);
        Date date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String result = sdf.format(date);
        return result;
    }


    /**
     *
     * @param startDate     选择的日期
     * @param mStartDate    日期格式
     * @return  和当天时间间隔
     */
    public static int getNumOfDays(String startDate,String endDate, String mStartDate) {
        boolean isBefore = true;

        SimpleDateFormat sdf = new SimpleDateFormat(mStartDate);
        try {
            isBefore = sdf.parse(startDate).after(sdf.parse(endDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return isBefore ? -1 : 1;
    }

    /**
     * 查询当前时间间隔i个月日期
     * @param i 正数为以后，负数为以前
     * @param pattern   日期格式
     * @return
     */
    public static String beforeMothDate(int i , String pattern){
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, i);
        Date m = c.getTime();
        String mon = format.format(m);
        return mon ;
    }

    /**
     * 获取当前月份的第一天
     * @return
     */
    public static String theFirstDayOfMonth(String pattern){
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        return format.format(c.getTime());
    }

    /**
     * 日期转星期
     *
     * @param date
     * @return
     */
    public static String DateToWeek(String date,String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date parse = null;
        try {
            parse = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parse);
        int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayIndex < 1 || dayIndex > Week_DAYS.length) {
            return null;
        }

        return Week_DAYS[dayIndex - 1];
    }
}